﻿/********************************************************
 * 类名： GoodsBLL.cs
 * 功能： 商品管理业务层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-12-11	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using PSS.DAL.SetManage.BasicData;
using PSS.Entity.SetManage.BasicData;
using PSS.Utility;

namespace PSS.BLL.SetManage.BasicData
{
    public class GoodsBLL
    {
        private readonly GoodsDAL _dal = new GoodsDAL();

        public void AddGoods(string data)
        {
            var model = new GoodsEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<GoodsDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.GoodsId = Guid.NewGuid();
                model.GoodsCode = row["GoodsCode"].ToString();
                model.GoodsName = row["GoodsName"].ToString();
                model.GCategoryId = new Guid(row["GCategoryId"].ToString());
                model.Specific = row["Specific"].ToString();
                model.StorageId = new Guid(row["StorageId"].ToString());
                model.UnitId = new Guid(row["UnitId"].ToString());
                model.PurchasePrice = Convert.ToDecimal(row["PurchasePrice"]);
                model.SalePrice = Convert.ToDecimal(row["SalePrice"]);
                model.Remark = row["Remark"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new GoodsDetailEntity();
                    cmodel.DetailId = Guid.NewGuid();
                    cmodel.GoodsId = model.GoodsId;
                    cmodel.StorageId = new Guid(row["StorageId"].ToString());
                    cmodel.InitialNumber = item["InitialNumber"] != null ? Convert.ToDecimal(item["InitialNumber"]) : 0;
                    cmodel.UnitCost = item["UnitCost"] != null ? Convert.ToDecimal(item["UnitCost"].ToString()) : 0;
                    cmodel.InitialPrice = Math.Round(cmodel.InitialNumber * cmodel.UnitCost, 4);
                    list.Add(cmodel);
                }
            }
            if (_dal.AddGoods(model))
            {
                _dal.AddGoodsDetail(list);
            }
        }

        public string GetGoodsList(GoodsQcEntity qcmodel)
        {
            var total = _dal.GetTotal(qcmodel);
            var list = _dal.GetGoodsList(qcmodel);
            return JsonHelper.Encode(total, list);
        }

        public string GetGoodsInfo(string id)
        {
            return JsonHelper.Encode(_dal.GetGoodsInfo(id));
        }

        public string GetGoodsDetailList(GoodsDetailQcEntity qcmodel)
        {
            var list = _dal.GetGoodsDetailList(qcmodel);
            return JsonHelper.Encode(list);
        }

        public void ModifyGoods(string data)
        {
            var model = new GoodsEntity();
            var rows = (ArrayList)JsonHelper.Decode(data);
            var list = new List<GoodsDetailEntity>();
            foreach (Hashtable row in rows)
            {
                model.GoodsId = new Guid(row["GoodsId"].ToString());
                model.GoodsCode = row["GoodsCode"].ToString();
                model.GoodsName = row["GoodsName"].ToString();
                model.GCategoryId = new Guid(row["GCategoryId"].ToString());
                model.Specific = row["Specific"].ToString();
                model.StorageId = new Guid(row["StorageId"].ToString());
                model.UnitId = new Guid(row["UnitId"].ToString());
                model.PurchasePrice = Convert.ToDecimal(row["PurchasePrice"]);
                model.SalePrice = Convert.ToDecimal(row["SalePrice"]);
                model.Remark = row["Remark"].ToString();
                model.ArrList = (ArrayList)row["arrList"];
                foreach (Hashtable item in model.ArrList)
                {
                    var cmodel = new GoodsDetailEntity();
                    cmodel.DetailId = Guid.NewGuid();
                    cmodel.GoodsId = model.GoodsId;
                    cmodel.StorageId = new Guid(item["StorageId"].ToString());
                    cmodel.InitialNumber = item["InitialNumber"] != null ? Convert.ToDecimal(item["InitialNumber"]) : 0;
                    cmodel.UnitCost = item["UnitCost"] != null ? Convert.ToDecimal(item["UnitCost"].ToString()) : 0;
                    cmodel.InitialPrice = item["InitialPrice"] != null ? Convert.ToDecimal(item["InitialPrice"].ToString()) : 0;
                    list.Add(cmodel);
                }
            }
            _dal.ModifyGoods(model);
            _dal.DeleteGoodsDetail(model.GoodsId.ToString());
            _dal.AddGoodsDetail(list);
        }

        public bool RemoveGoods(string id)
        {
            var result = false;
            if (_dal.RemoveGoods(id))
            {
                _dal.DeleteGoodsDetail(id);
                result = true;
            }
            return result;
        }
    }
}
